<?php
namespace ImiApp\ApiServer\Controller;

use Imi\Controller\HttpController;
use Imi\Server\Route\Annotation\Action;
use Imi\Server\Route\Annotation\Controller;
use ImiApp\ApiServer\Model\Test;

/**
 * @Controller("/model/")
 */
class ModelController extends HttpController
{
    /**
     * @Action
     *
     * @param string $a
     * @return void
     */
    public function create($a)
    {
        $record = Test::newInstance();
        $record->a = $a;
        // $record->save();
        $result = $record->insert();
        return [
            // 'id'    =>  $result->getLastInsertId(),
            'id'    =>  $record->id,
        ];
    }

    /**
     * @Action
     *
     * @param int $id
     * @param string $a
     * @return void
     */
    public function update($id, $a)
    {
        $record = Test::find($id);
        $record->a = $a;
        // $record->save();
        $result = $record->update();
        return [
            'rowCount'  =>  $result->getAffectedRows(),
        ];
    }

    /**
     * @Action
     *
     * @param int $id
     * @return void
     */
    public function get($id)
    {
        $record = Test::find($id);
        return [
            'data'  =>  $record,
        ];
    }

    /**
     * @Action
     *
     * @return void
     */
    public function query()
    {
        // $list = Test::select();
        $list = Test::query()->select()->getArray();
        return [
            'list'  =>  $list,
        ];
    }

    /**
     * @Action
     *
     * @param int $id
     * @return void
     */
    public function delete($id)
    {
        $record = Test::find($id);
        $result = $record->delete();
        return [
            'rowCount'  =>  $result->getAffectedRows(),
        ];
    }

}
